package com.earthchen.service.user.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.earthchen.dao.user.DeptMapper;
import com.earthchen.domain.entity.user.Dept;
import com.earthchen.service.user.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.earthchen.constant.*;

/**
 * @author earthchen
 * @date 2018/10/8
 **/
@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements DeptService
{
    @Autowired
    private DeptMapper deptMapper;

    @Override
    public List<Dept> getParentDept()
    {
        return deptMapper.getParentDept();
    }

    /**
     * 获得子部门
     *
     * @return
     */
    @Override
    public List<Dept> getChildrenDept(Long parentId)
    {
        return deptMapper.getChildrenDept(parentId);
    }

    @Override
    public Dept selectDeptById(Long deptId)
    {
        return deptMapper.selectDeptById(deptId);
    }

    @Override
    public List<Map<String, Object>> selectDeptTree()
    {
        List<Map<String, Object>> trees = new ArrayList<>();
        List<Dept> deptList = list(new QueryWrapper<>());

        for (Dept dept : deptList)
        {
            if (UserConstants.DEPT_NORMAL.equals(dept.getStatus()))
            {
                Map<String, Object> deptMap = new HashMap<>();
                deptMap.put("id", dept.getId().toString());
                deptMap.put("pId", dept.getParentId().toString());
                deptMap.put("name", dept.getDeptName());
                deptMap.put("title", dept.getDeptName());
                trees.add(deptMap);
            }
        }
        return trees;
    }
}
